import modules from './modules'
import Vue from 'vue'
import {
	RouterMount,
	createRouter
} from 'uni-simple-router';
import {
	isLoginFailure
} from '@/utils/common.js'

//初始化
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routerErrorEach: ({
		type,
		level,
		...args
	}) => {
		console.log({
			type,
			level,
			...args
		})
		// #ifdef APP-PLUS
		if (type === 3) {
			router.$lockStatus = false;
			uni.showModal({
				title: '提示',
				content: '您确定要退出应用吗？',
				success: function(res) {
					if (res.confirm) {
						plus.runtime.quit();
					}
				}
			});
		}
		// #endif
	},
	routes: [...modules] //路由表
});

const whiteList = ['/pages/login/login']
//全局路由前置守卫
router.beforeEach((to, from, next) => {
	const {
		path
	} = to

	//	如果是白名单中的页面，不进行任何操作
	if (whiteList.some(url => path.includes(url))) {
		next()
	} else {
		//	如果登录失效，则跳转登录页
		if (isLoginFailure()) {
			next({
				path: '/pages/login/login'
			})
		} else {
			uni.setStorageSync('time', new Date().getTime())
			next()
		}
	}


})
// 全局路由后置守卫
router.afterEach((to, from) => {
	console.log("afterEach")
})

export default {
	router,
	RouterMount
};
